var editor;
$(function(){
	kedit('textarea[name="content"]');
	$("#confirmAddArticle").click(function(){
		var title=$("#title").val();
		var author=$("#author").val();
		var atype=$("#atype").val();
		var content=editor.html();  
		content=encodeURI(content);
		var pic;
		$.ajaxFileUpload({
			  url:rootPath+"/article/addOrUpdateArticle",//需要链接到服务器地址   
			  type:'post',
			  data:{'title':title,'author':author,'atype':atype,'content':content},
		      secureuri:false,  
		      fileElementId:"pic",//文件选择框的id属性  
		      dataType: 'JSON',   //json 
		      success: function (data) {  
		    	  var reg = /<pre.+?>(.+)<\/pre>/g;  
		    	  var result = data.match(reg);  
		    	  data = RegExp.$1;
		    	  data=toJson(data);
		    	  if (data.resultSuccess) {
		    		  layer.msg('添加成功');
					} else {
						layer.alert('添加失败！', 3);
					}
		    	  
		      }
		})
	})
	function getContent(editor){
	    editor.sync(); 
	}
})
function kedit(kedit){
		editor = KindEditor.create(kedit, {
			resizeType : 1,
			imageSizeLimit : '5MB', //批量上传图片单张最大容量
	    imageUploadLimit : 20, //批量上传图片同时上传最多个数
		allowPreviewEmoticons : true,
		allowImageUpload : true,
		fullscreenShortcut : false,
		width: '100%',
		uploadJson:rootPath+'/commonImage/uploadImage/article',
		items : [
		 		'source', '|', 'undo', 'redo', '|', 'preview', 'cut', 'copy', 'paste',
		 		'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
		 		'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
		 		'superscript', 'clearhtml', 'quickformat', '|', 'fullscreen', '/',
		 		'formatblock', 'title', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', '|','bold',
		 		'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
		 		'media','table', 'hr', 'emoticons', 'pagebreak', 'link', 'unlink'
		],
		afterBlur:function(){	//此方法保证能传递数据到后台
			this.sync();
		}
	});
}
function toJson(str){
  var json = eval('(' + str + ')');
  return json;
 }

